/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.gui;

import br.com.controllers.Controller;
import br.com.models.Days;
import br.com.models.Graduation;
import br.com.models.Teacher;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.text.ParseException;
import java.util.ArrayList;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.text.MaskFormatter;

/**
 *
 * @author Valdigleis
 */
public class SignTeacher extends javax.swing.JDialog {

    private JFrame parent;
    
    private MaskFormatter formatterName = new MaskFormatter();
    
    private MaskFormatter formatterRegister = new MaskFormatter();
    
    private Teacher teacher;
    
    private Graduation graduation;
    
    private ArrayList<Days> days;
    /**
     * Creates new form SignTeacher
     */
    public SignTeacher(JFrame parent) {
        super(parent, true);
        initComponents();
        try{
            formatterName.setMask("U*****************************");
            formatterName.setInvalidCharacters("0123456789!?/|[]{}ªº°");
            formatterName.install(nameTeacher);
            formatterRegister.setMask("######");
            formatterRegister.install(registerProfessor);
        }catch(ParseException e){
            System.out.println(e.getMessage());
        }
        this.parent = parent;
        this.teacher = new Teacher();
        this.graduation = new Graduation();
        this.days = new ArrayList<Days>();
        this.statusFormation.addItem("Em Andamento");
        this.statusFormation.addItem("Concluído");
        this.statusFormation.addItem("Abandonado");
        this.statusFormation.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
               if(statusFormation.getSelectedIndex() != 0){
                   timeFormation.setSelectedIndex(0);
                   timeFormation.setEnabled(false);
               }else{
                   timeFormation.setEnabled(true);
               }
            }
                         
        });
        setResizable(false);
        Rectangle parentBounds = parent.getBounds();
        Dimension size = getSize();
        // Center in the parent
        int x = Math.max(0, parentBounds.x + (parentBounds.width - size.width) / 2);
        int y = Math.max(0, parentBounds.y + (parentBounds.height - size.height) / 2);
        setLocation(new Point(x, y));
        setVisible(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        levelFormation = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        statusFormation = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        timeFormation = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        day1 = new javax.swing.JRadioButton();
        day2 = new javax.swing.JRadioButton();
        day3 = new javax.swing.JRadioButton();
        day4 = new javax.swing.JRadioButton();
        day5 = new javax.swing.JRadioButton();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        registerProfessor = new javax.swing.JFormattedTextField();
        nameTeacher = new JFormattedTextField(formatterName);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Ficha de cadastro de Professor");

        jLabel1.setText("Nome:");

        jLabel2.setText("Matrícula:");

        jLabel3.setText("Formação Atual");

        levelFormation.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Mestrado", "Doutorado", "Pós-doutrorado" }));
        levelFormation.setToolTipText("Selecione o ultimo nivel formação cursada ou cursando.");

        jLabel4.setText("Status Atual");

        statusFormation.setToolTipText("Escolha a situação do curso de formação.");
        statusFormation.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                statusFormationActionPerformed(evt);
            }
        });

        jLabel5.setText("Tempo de conclusão");

        timeFormation.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "0", "1", "2", "3", "4", "5", "6", "7", "8" }));
        timeFormation.setToolTipText("Periodo até concluir a formação ou 0 se a mesma tiver sido concluida ou abandonada.");
        timeFormation.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                timeFormationActionPerformed(evt);
            }
        });

        jLabel6.setText("Selecione os dias em que este professor pode lecionar suas aulas.");

        day1.setText("Segunda");

        day2.setText("Terça");

        day3.setText("Quarta");

        day4.setText("Quinta");

        day5.setText("Sexta");

        jButton1.setText("Gravar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Cancelar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        registerProfessor.setToolTipText("Entre com a matricula do professor.");
        registerProfessor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                registerProfessorActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(levelFormation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(statusFormation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(timeFormation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addComponent(jLabel1)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(nameTeacher))
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addComponent(jLabel2)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(registerProfessor, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(0, 2, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addComponent(day1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(day2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(day3))
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(day4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(day5))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jButton2)))
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(nameTeacher, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(registerProfessor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel3)
                        .addComponent(jLabel5)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(levelFormation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(statusFormation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(timeFormation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(day1)
                    .addComponent(day2)
                    .addComponent(day3)
                    .addComponent(day4)
                    .addComponent(day5))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void statusFormationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_statusFormationActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_statusFormationActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
       this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        this.days.clear();
        if(infosTeacher() && infosGraduation() && infosDays()){
            teacher.setDays(days);
            Controller controller = new Controller();
            if(controller.checkRegister(teacher.getRegister())){
                JOptionPane.showMessageDialog(parent, "Esta matricula já está em uso por outro professor");
            }
            else{
                controller.saveTeacher(teacher);
                JOptionPane.showMessageDialog(parent, "Professor cadastrado com sucesso!");
                this.dispose();
            }
            
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private boolean infosTeacher(){
        //coletando informações do professor
       this.teacher.setName(nameTeacher.getText().trim());
       this.teacher.setRegister(registerProfessor.getText().trim());
       if(teacher.getName().equals("") || teacher.getName().equals(null)){
           JOptionPane.showMessageDialog(parent, "Por favor insira o nome e a matricula do professor");
           return false;
       }
       if(teacher.getRegister().equals("") || teacher.getRegister().equals(null)){
           JOptionPane.showMessageDialog(parent, "Por favor insira o nome e a matricula do professor");
           return false;
       }
       else{
           return true;
       }
    }
    
    private boolean infosGraduation(){
        //Colentanto informações da graduation
       this.graduation.codeLevel(levelFormation.getSelectedIndex());
       this.graduation.codeSituation(statusFormation.getSelectedIndex());
       this.graduation.setTime(timeFormation.getSelectedIndex());
       this.teacher.setGraduation(this.graduation);
       return true;
    }
    
    private boolean infosDays(){
        //colentando o expediente
       if(day1.isSelected()){
           Days day = new Days();
           day.setName("Segunda-feira");
           this.days.add(day);
       }
       if(day2.isSelected()){
           Days day = new Days();
           day.setName("Terça-feira");
           this.days.add(day);
       }
       if(day3.isSelected()){
           Days day = new Days();
           day.setName("Quarta-feira");
           this.days.add(day);
       }
       if(day4.isSelected()){
           Days day = new Days();
           day.setName("Quinta-feira");
           this.days.add(day);
       }
       if(day5.isSelected()){
           Days day = new Days();
           day.setName("Sexta-feira");
           this.days.add(day);
       }
       if(days.isEmpty()){
           JOptionPane.showMessageDialog(parent, "Selecione pelo menos um dia de expediente.");
           return false;
       }else{
           return true;
       }  
    }
    
    
    private void timeFormationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_timeFormationActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_timeFormationActionPerformed

    private void registerProfessorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_registerProfessorActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_registerProfessorActionPerformed

   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton day1;
    private javax.swing.JRadioButton day2;
    private javax.swing.JRadioButton day3;
    private javax.swing.JRadioButton day4;
    private javax.swing.JRadioButton day5;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JComboBox levelFormation;
    private javax.swing.JFormattedTextField nameTeacher;
    private javax.swing.JFormattedTextField registerProfessor;
    private javax.swing.JComboBox statusFormation;
    private javax.swing.JComboBox timeFormation;
    // End of variables declaration//GEN-END:variables
}
